<?php
/*
* LoginController.php
* Handles the flow of the login.
*/

require_once 'LoginModel.php';
require_once 'LoginView.php';

class LoginController
{
	public function DoControll()
	{
		$html = '';
	    
		$loginView = new LoginView();
		$loginModel = new LoginModel();
		
		// If the user isn't logged in
		if ( ! $loginView->IsLoggedIn() )
		{
			// Show the login form.
			$html .= $loginView->DoLoginForm();
			
			// Has the user tried to login
			if ( $loginView->TriedToLogin() )
			{
				// Check the credentials, Login the user.
				if ( $loginModel->AuthenticateUser( $loginView->GetPostUsername(), $loginView->GetPostPassword() ) )
				{
					$html .= $loginView->DoLogOutButton();
				}
				// Invalid username and/or password.
				else
				{
					$html .= 'Invalid username and/or password.';
				}
			}
		}
		// User already logged in.
		else 
		{
			$html .= $loginView->DoLogOutButton();
			
			// Checks if the user tried to Logout.
			if ( $loginView->TriedToLogout() )
			{
				// Logout the user.
				$loginView->Logout();
			}
		}
		
		return $html;
	}
}
